import axios from "axios";


// 获取网页中的图片链接
export async function GetImages(baseUrl:string){
    return GetFileURL(baseUrl,".png");
};


// 获取链接中的所有文件链接
export async function GetFileURL(baseUrl:string,fileSuffix:string){
  let urlsArray;

  try {
    const response = await axios.get(baseUrl);

    const html = response.data;

    let urls;
    // NOTE:暂时只支持png及glb
    switch(fileSuffix){
      case ".png":
        urls = [...html.matchAll(/href="([^"]+\.png)"/g)].map(
          (match) => baseUrl + match[1]
        );
        break;
      case ".glb":
        urls = [...html.matchAll(/href="([^"]+\.glb)"/g)].map(
          (match) => baseUrl + match[1]
        );
        break;
    }
    // console.log("urls:", urls);

    if (urls.length > 0) {
      urlsArray = urls;
    }

  } catch (error) {
    console.error("获取图片失败:", error);
    urlsArray = [];

  }

  return urlsArray;
};